var securityKeyboard_Fill_Div_id="";
cipherMode=1;
$(function(){
	
	for (var i = 0; i < 3; i++) {
		securityKeyboard_Fill_Div_id="securityKeyboard_Fill_Div_"+parseInt(10000*Math.random());
		var securityKeyboard_Fill_Div=$("#"+securityKeyboard_Fill_Div_id);
		if(securityKeyboard_Fill_Div.length==0){
			break;
		}
	}
	var securityKeyboard_Fill_Div=$("#"+securityKeyboard_Fill_Div_id);
	if(securityKeyboard_Fill_Div.length==0){
		securityKeyboard_Fill_Div=$("<div style='width: 100%; height: 1px;' id='"+securityKeyboard_Fill_Div_id+"'></div>");
	}
	$("body").append(securityKeyboard_Fill_Div);
})
$.getSecurityKeyboard=function(url,type){
	if(type != 'Number' && type != 'standard'){
		return ;
	}
	var securityKeyboard={
			type:type,
			screenSize:fn(),
			/**
			  	{
					elementId:"PasswordS",
					placeholder:"请输入密码",
					length:12
				}
			 */
			init:function(data){
				$("#"+data["elementId"]).attr("type","hidden");
				var div=$("<div tabindex=1></div>");
				div.attr("class","ccsk-input Number placeholder");
				div.attr("id", data["elementId"]+"_keyboardinput");
				div.css("width", "200px");
				div.css("height", "34px");
				div.text(data["placeholder"]);
				div.data("alt", data["placeholder"]);
				div.data("input", data["elementId"]);
				div.data("length", data["length"]);
				div.bind("touchstart",function(){
					var Y = div.offset().top+parseInt(div.css("height"))+20;
					var height=$(window).height();
					var divHeight=parseInt(securityKeyboard.securityKeyboardDiv.css("height"));
					var keshi=height-divHeight;	//屏幕高度减去键盘高度 等于可视高度；
					if(keshi<Y){
						var dheight=$(document.body).height();
						var gong=Y-keshi;
						if(dheight<height){
							gong+=height-dheight;
						}						
						var securityKeyboard_Fill_Div=$("#"+securityKeyboard_Fill_Div_id);
						securityKeyboard_Fill_Div.css("height", gong+"px");
						$("html,body").animate({scrollTop:gong},"slow");
					}
					
					
					
					
					$(securityKeyboard.inputDivItem).each(function(i,o) {
						if(o==div){
							div.data("isBlur","true");
							o.blur();
						}
					});
					if(div.hasClass("placeholder")){
						div.text("|");	
					}
					div.removeClass("placeholder");
					div.addClass("focus");
					setTimeout(function(){
						securityKeyboard.securityKeyboardDiv.show();
					},200);  
					
					div.focus();
					securityKeyboard.inputDiv=div;
					
				});
				if(securityKeyboard.inputDivItem == undefined){
					securityKeyboard.inputDivItem=new Array();
				}
				securityKeyboard.inputDivItem.push(div);
				$("#"+data["elementId"]).after(div);
				//失去焦点不隐藏
//				div.blur( function (e) {
//					if(div.data("isKey")=="true"){
//						div.data("isKey","");
//						div.focus();
//					}else{
//						if(div.data("pwd") == undefined || div.data("pwd").length==0){
//							div.addClass("placeholder");
//							div.text(div.data("alt"));
//						}
//						div.removeClass("focus");
//						if(div.data("isBlur")=="true"){
//							div.data("isBlur","");
//						}else{
//							securityKeyboard.securityKeyboardDiv.hide("slow");
//						}
//							
//					}
//				} );
				div.equals=function(div){
					var pwd1=this.data("pwd");
					var pwd2=div.data("pwd");
					if(pwd1 == null && pwd2 == null){
						return true;
					}else if(pwd1 == null && pwd2 != null){
						return false;
					}else if(pwd1 != null && pwd2 == null){
						return false;
					}else if(pwd1.length != pwd2.length){
						return false;
					}else{
						for(var i = 0 ; i < pwd1.length;i++){
							if(pwd1[i]!=pwd2[i]){
								return false;
							}
						}
						return true;
					}					
				}
				div.getLength=function(){
					if(this.data("pwd") == undefined){
						return 0;
					}
					return this.data("pwd").length;
				}
				return div;
			},
			function clear(){
				for(var inputDiv : securityKeyboard.inputDivItem){
					$(inputDiv).remove();
				}
			}
	}
	securityKeyboard.securityKeyboardDiv = $(document.createElement("div"));
	securityKeyboard.securityKeyboardDiv.attr("id", "securityKeyboard_"+type);
	securityKeyboard.securityKeyboardDiv.addClass("ccsk-warp slide-in-up ng-leave-active ng-enter ng-enter-active");
	securityKeyboard.securityKeyboardDiv.css("width", securityKeyboard.screenSize[0]+"px");
	securityKeyboard.securityKeyboardDiv.css("height", parseInt(securityKeyboard.screenSize[1]*0.41)+"px");
	securityKeyboard.securityKeyboardDiv.css("bottom","0px");
	securityKeyboard.securityKeyboardDiv.hide();
    
	securityKeyboard.img = $(document.createElement("img"));
	securityKeyboard.img.attr("id", "keyboardimg_"+type);
	securityKeyboard.img.attr("alt", "密码键盘");
	securityKeyboard.img.addClass("ccsk-img "+type+" ccsk-plugin "+type+"_plugin0");
	securityKeyboard.img.attr("width", securityKeyboard.screenSize[0]);
    securityKeyboard.img.attr("height", parseInt(securityKeyboard.screenSize[1]*0.41));
    securityKeyboard.securityKeyboardDiv.append(securityKeyboard.img);
    securityKeyboard.touchstart=function(event){
		securityKeyboard.inputDiv.data("isKey","true");
		
    	var index=$(event.target).attr("index");
		var pwd=securityKeyboard.inputDiv.data("pwd");
		if(pwd  == undefined ){
			pwd = new Array();
		}
		if(index == -1){ //完成
			if(securityKeyboard.inputDiv.data("pwd") == undefined || securityKeyboard.inputDiv.data("pwd").length==0){
				securityKeyboard.inputDiv.addClass("placeholder");
				securityKeyboard.inputDiv.text(securityKeyboard.inputDiv.data("alt"));
			}
			securityKeyboard.inputDiv.removeClass("focus");
			securityKeyboard.securityKeyboardDiv.hide("slow");
			return;
		}else if(index == -2){//删除
			pwd.pop();
		}else if(index == -3){//转小写键盘
			securityKeyboard.createLetterKeyboard("y");
		}else if(index == -4){//转大写键盘
			securityKeyboard.createLetterKeyboard("X");			
		}else if(index == -5){//转数字键盘
			securityKeyboard.createExtendNumberKeyboard();
		}else if(index == -6){//转符号键盘
			securityKeyboard.createSymbolKeyboard();
		}else{                 //输入
			if(pwd.length==securityKeyboard.inputDiv.data("length")){
				return;
			}
			pwd.push(index);
		}
		securityKeyboard.inputDiv.data("pwd",pwd);
		var txt="";
		$(pwd).each(function(){
			txt+="*";
		});
		securityKeyboard.inputDiv.text(txt+"|");
		var encryptedPasswd="";
		if(securityKeyboard.encryption_Type == "RSA" ){
			var encrypt = new JSEncrypt();
			encrypt.setPublicKey(securityKeyboard.key);
			encryptedPasswd=encrypt.encrypt(pwd.join("")+new Date().getTime()); 
		}else if(securityKeyboard.encryption_Type == "SM2" ){
			encryptedPasswd = sm2Encrypt(pwd.join("")+new Date().getTime(), securityKeyboard.key);
		}
		
		encryptedPasswd+="!#!"+securityKeyboard.UUID;
		$("#"+securityKeyboard.inputDiv.data("input")).val(encryptedPasswd);
		
    }
	if(type == 'Number'){
		securityKeyboard.createSecurityKeyboard=numberSecurityKeyboard;
	}
	if(type == 'standard'){
		securityKeyboard.createSecurityKeyboard=standardSecurityKeyboard;
	}
    $.get(url,{type:type,r:Math.random(),method: 'dbk.provideServices.securityKeyboard',v:"aa"},function (pair){
		var data = pair.split("!#!");
		securityKeyboard.createSecurityKeyboard(data[0]);
		securityKeyboard.UUID=data[1];
		securityKeyboard.key=data[2];
		securityKeyboard.encryption_Type=data[3];
	});
    $("body").append(securityKeyboard.securityKeyboardDiv);
    return securityKeyboard;
}
function standardSecurityKeyboard(image){
	var timage=image.split(";$;");
	this.yLetterImage=timage[0];
	this.yLetterImageKey=timage[1];
	this.XLetterImage=timage[2];
	this.XLetterImageKey=timage[3];
	this.numberKeyImage=timage[4];
	this.numberKeyImageKey=timage[5];
	this.symbolImage=timage[6];
	this.symbolImageKey=timage[7];
	this.createLetterKeyboard=createLetterKeyboard;
	this.createExtendNumberKeyboard=createExtendNumberKeyboard;
	this.createSymbolKeyboard=createSymbolKeyboard;
	this.createLetterKeyboard('y');
//	this.createSymbolKeyboard();
}

function createSymbolKeyboard(){
	this.img.attr("src","data:image/png;base64,"+this["symbolImage"]);
	this.securityKeyboardDiv.find("div").detach();
	var width=this.screenSize[0]/10.6;
	var height=parseInt(this.screenSize[1]*0.41)/4.4;
	var jleft=this.screenSize[0]/153
	var jtop=parseInt(this.screenSize[1]*0.41)/55;
	var key=this["symbolImageKey"];
	for(var i=0;i<10;i++){
		var left=i*width+(i*jleft);
		this.securityKeyboardDiv.append(createMenu(key[i],jtop,left,width,height));
	}
	for(var i=0;i<9;i++){
		var left=(this.screenSize[0]/20)+i*width+(i*jleft);
		this.securityKeyboardDiv.append(createMenu(key[i+10],((jtop*2)+height),left,width,height));
	}
	width=this.screenSize[0]/7.9;
	this.securityKeyboardDiv.append(createMenu('-5',((2+1)*jtop+(2*height)),1*jleft,width,height));
	
	width=this.screenSize[0]/10.6;
	var bleft=this.screenSize[0]/5.1;
	for(var i=0;i<5;i++){
		var left=(this.screenSize[0]/20)+i*width+(i*jleft)+bleft;
		this.securityKeyboardDiv.append(createMenu(key[i+10+9],((jtop*3)+(height*2)),left,width,height));
	}
	left=(this.screenSize[0]*0.865);
	width=this.screenSize[0]/8;
	this.securityKeyboardDiv.append(createMenu('-2',((jtop*3)+(height*2)),left,width,height));
	left=(this.screenSize[0]/95);
	width=this.screenSize[0]/4;
	this.securityKeyboardDiv.append(createMenu('-3',((jtop*4)+(height*3)),left,width,height));
	left=(this.screenSize[0]/1.35);
	width=this.screenSize[0]/4;
	this.securityKeyboardDiv.append(createMenu('-1',((jtop*4)+(height*3)),left,width,height));
	
	this.securityKeyboardDiv.find("div").bind("touchstart",this.touchstart);
}

function createExtendNumberKeyboard(){
	this.img.attr("src","data:image/png;base64,"+this["numberKeyImage"]);
	this.securityKeyboardDiv.find("div").detach();

	var width=this.screenSize[0]/5.4;
	var height=parseInt(this.screenSize[1]*0.41)/4.45;
	var bleft=this.screenSize[0]/48;
	
	var jleft=this.screenSize[0]/130;
	var jtop=parseInt(this.screenSize[1]*0.41)/55;

	for(var i = 0 ; i < 2 ; i++ ){
		for(var j = 0 ; j < 5 ; j++ ){
			this.securityKeyboardDiv.append(createMenu(this.numberKeyImageKey[(i*5)+j],((i+1)*jtop+(i*height)),(bleft+((j*jleft)+(j*width))),width,height));
		}
	}
	width=this.screenSize[0]/7.9;
	height=parseInt(this.screenSize[1]*0.41)/4.4;
	this.securityKeyboardDiv.append(createMenu('-6',((2+1)*jtop+(2*height)),(1*jleft),width,height));
	bleft=this.screenSize[0]/6.6;
	width=this.screenSize[0]/10.5;
	jleft=this.screenSize[0]/200;
	for(var i = 0 ; i < 7 ; i++ ){
		this.securityKeyboardDiv.append(createMenu(this.numberKeyImageKey[(2*5)+i],((2+1)*jtop+(2*height)),(bleft+((i*jleft)+(i*width))),width,height));
	}
	left=(this.screenSize[0]*0.865);
	width=this.screenSize[0]/8;
	this.securityKeyboardDiv.append(createMenu('-2',((jtop*3)+(height*2)),left,width,height));
	left=(this.screenSize[0]/95);
	width=this.screenSize[0]/4;
	this.securityKeyboardDiv.append(createMenu('-3',((jtop*4)+(height*3)),left,width,height));
	left=(this.screenSize[0]/1.35);
	width=this.screenSize[0]/4;
	this.securityKeyboardDiv.append(createMenu('-1',((jtop*4)+(height*3)),left,width,height));
	
	this.securityKeyboardDiv.find("div").bind("touchstart",this.touchstart);
}
function createLetterKeyboard(c){
	this.img.attr("src","data:image/png;base64,"+this[c+"LetterImage"]);
	this.securityKeyboardDiv.find("div").detach();
	var width=this.screenSize[0]/10.6;
	var height=parseInt(this.screenSize[1]*0.41)/4.4;
	var jleft=this.screenSize[0]/153
	var jtop=parseInt(this.screenSize[1]*0.41)/55;
	var key=this[c+"LetterImageKey"];
	for(var i=0;i<10;i++){
		var left=i*width+(i*jleft);
		this.securityKeyboardDiv.append(createMenu(key[i],jtop,left,width,height));
	}
	for(var i=0;i<9;i++){
		var left=(this.screenSize[0]/20)+i*width+(i*jleft);
		this.securityKeyboardDiv.append(createMenu(key[i+10],((jtop*2)+height),left,width,height));
	}
	for(var i=0;i<7;i++){
		var left=(this.screenSize[0]/6.7)+i*width+(i*jleft);
		this.securityKeyboardDiv.append(createMenu(key[i+19],((jtop*3)+(height*2)),left,width,height));
	}
	var left=(this.screenSize[0]/95);
	width=this.screenSize[0]/8;
	if(c=='y'){
		this.securityKeyboardDiv.append(createMenu('-4',((jtop*3)+(height*2)),left,width,height));
	}else{
		this.securityKeyboardDiv.append(createMenu('-3',((jtop*3)+(height*2)),left,width,height));		
	}
	
	left=(this.screenSize[0]*0.865);
	width=this.screenSize[0]/8;
	this.securityKeyboardDiv.append(createMenu('-2',((jtop*3)+(height*2)),left,width,height));
	left=(this.screenSize[0]/95);
	width=this.screenSize[0]/4;
	this.securityKeyboardDiv.append(createMenu('-5',((jtop*4)+(height*3)),left,width,height));
	
	left=(this.screenSize[0]/1.35);
	width=this.screenSize[0]/4;
	this.securityKeyboardDiv.append(createMenu('-1',((jtop*4)+(height*3)),left,width,height));	
	this.securityKeyboardDiv.find("div").bind("touchstart",this.touchstart);
}

function createMenu(index,top,left,width,height){
	var div = $(document.createElement("div"));
	div.addClass("ccsk-area");
	div.attr("index",index);
	div.css("top",top+"px");
	div.css("left",left+"px");
	div.css("width",width+"px");
	div.css("height",height+"px");
	return div;
}

function numberSecurityKeyboard(data){
	data=data.split(";$;");
	this.img.attr("src","data:image/png;base64,"+data[0]);
	var width=this.screenSize[0]/3;
	var height=parseInt(this.screenSize[1]*0.41)/4;
	for(var i = 0 ;i < 4 ; i++){
		for(var j = 0 ;j < 3 ; j++){
			var top=3+i*height;
			var left=3+j*width;
			if(i==3){
				if(j==0){
					this.securityKeyboardDiv.append(createMenu('-1',top,left,width,height));
				}else if(j==1){
					this.securityKeyboardDiv.append(createMenu((data[1][9]),top,left,width,height));
				}else if(j==2){
					this.securityKeyboardDiv.append(createMenu('-2',top,left,width,height));
				}
			}else{
				this.securityKeyboardDiv.append(createMenu((data[1][i*3+j]),top,left,width,height));
			}
			
		}	
	}
	var securityKeyboard=this;
	this.securityKeyboardDiv.find("div").bind("touchstart",securityKeyboard.touchstart);
}